package com.diploma.interaction.controller;

import com.alibaba.fastjson.JSON;
import com.diploma.interaction.model.DmQxSwrysfTest;
import com.diploma.interaction.model.WxxcxDlzhxx;
import com.diploma.interaction.model.WxxcxHdxx;
import com.diploma.interaction.service.HdxxService;
import com.diploma.interaction.service.SwrysfService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping(value = "/v1/interaction")
@Slf4j
public class InteractionController {

    @Autowired
    HdxxService hdxxService;

    @Autowired
    SwrysfService swrysfService;

    @RequestMapping(value = "/getInternativeMessageList", method = RequestMethod.GET)
    String getInternativeMessageList(HttpSession session, Model model) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            model.addAttribute("hdxx", hdxxService.selectByStaffId(staff.getSwryDm()));
            return "interaction";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }

    @RequestMapping(value = "/setInternativeMessage", method = RequestMethod.GET)
    String setInternativeMessage(@RequestParam("id") int id,Model model, HttpSession session) {
        WxxcxHdxx hd=hdxxService.selectByXxxh(id);
        model.addAttribute("message", hd);
        session.setAttribute("messageId",id);
        return "setInteraction";
    }

    @RequestMapping(value = "/staffSetInternativeMessage", method = RequestMethod.POST)
    String staffSetInternativeMessage(WxxcxHdxx message, HttpSession session, Model model ) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            message.setHfrq(new Date());
            message.setSsglyDm(staff.getSwryDm());
            //获得税务局名称
            DmQxSwrysfTest swry=swrysfService.selectById(staff.getSwryDm());
            log.info("税收人员身份,swry={}", swry);
            String rysfmc= (String) swry.getRysfmc();
            String [] arr = rysfmc.split("\\s+");
            message.setSsryDm(staff.getSwryDm());
            message.setSsryXm(arr[0]);
            message.setSwjgMc(arr[1]);
            hdxxService.updateByIdSelective(message, (Integer) session.getAttribute("messageId"));
            model.addAttribute("hdxx", hdxxService.selectByStaffId(staff.getSwryDm()));
            return "interaction";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }

    @RequestMapping(value = "/deleteInternativeMessage", method = RequestMethod.GET)
    String deleteInternativeMessage(@RequestParam("id") int id,Model model, HttpSession session ) {
        WxxcxDlzhxx staff= (WxxcxDlzhxx) session.getAttribute("staff");
        if (staff!=null) {
            hdxxService.deleteById(id);
            model.addAttribute("hdxx", hdxxService.selectByStaffId(staff.getSwryDm()));
            return "interaction";
        } else {
            return "redirect:/v1/login/notVerify";
        }

    }

}
